<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>admin2.0</title>
    <meta name="renderer" content="webkit">
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script type="text/javascript" src="js/common.js"></script>
    <script type="text/javascript" src="js/formedit.js"></script>
  </head>
  
  <body>
    <div class="x-body">
        <form id="fm" class="layui-form">
          <input type="hidden" name="id"/>
          <div class="layui-form-item">
              <label for="L_username" class="layui-form-label">
                  <span class="x-red">*</span>职位名称
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="L_username" name="jobname" required="" lay-verify="jobname"
                  autocomplete="off" class="layui-input">
              </div>
          </div>
          
          <div class="layui-form-item">
              <label for="L_pass" class="layui-form-label">
                  <span class="x-red">*</span>职位描述
              </label>
              <div class="layui-input-inline">
                  <textarea  name="jobdetail" required="" lay-verify="jobdetail"
                   class="layui-textarea"></textarea>
              </div>
          </div>
          
          <div class="layui-form-item">
              <label for="L_email" class="layui-form-label">
                  <span class="x-red">*</span>所属医馆
              </label>
              <div class="layui-input-inline">
                   <select name="hospitalid" lay-verify="">
					</select>   
              </div>
          </div>
          
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">保存</button>
          </div>
      </form>
    </div>
    <script type="text/javascript">
        $(function(){
        	/**请求所有的医馆信息*/
            $.post("/hospitals/search",function(res){
                for(let i=0;i<res.rows.length;i++){
                  $("select[name='hospitalid']").append(`<option value="${res.rows[i].id}">${res.rows[i].hname}</option>`);
                  layui.use(['layer', 'form'], function(){
                      var layer = layui.layer
                      ,form = layui.form;
                      form.render('select');
                  });
                }
                
            })
        })
    </script>
    
    
    <script>
      layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
        ,layer = layui.layer;
      
        //自定义验证规则
        form.verify({
        	jobname: function(value){
                if(value.length < 1){
                  return '不能为空';
                }
            },
            jobdetail: function(value){
              if(value.length < 1){
                return '不能为空';
              }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
        	  var formData=JSON.stringify(data.field);
	          console.log(formData);
	          //发异步，把数据提交给后台
	          $.ajax({
	          	type:"post",
	          	url:"/job/insert",
	          	data:formData,
       			contentType: "application/json;charset=utf-8",
       			dataType: "json",//预期服务器返回的数据类型
       			async:false,
       			success:function(result){
       				console.log(result);//打印服务端返回的数据
       				layer.alert("保存成功", {icon: 6},function () {
	                // 获得frame索引
	                var index = parent.layer.getFrameIndex(window.name);
	                //关闭当前frame
	                parent.layer.close(index);
	                //刷新父页面表格
	                parent.layui.table.reload('idTest',{page: {curr: 1}});//idTest是表格的id；
          		});
       			},
       			error:function(){
       				layer.alert("保存失败", {icon: 5});
       			}
	          });
	          return false;
	        });
      });
  </script>
  </body>

</html>